package com.liunian.algorithmstudy.tree;

public class SumOfLeftLeaves404 extends TreeNode {
	int sum = 0;
	public int sumOfLeftLeaves(TreeNode root) {
		travelsal(root);
		return sum;
	}

	public int sumOfLeftLeaves3(TreeNode root) {
		if (root == null) {
			return 0;
		}
		int l = sumOfLeftLeaves3(root.left);
		int r = sumOfLeftLeaves3(root.right);
		if (root.left != null && root.left.left == null && root.left.right == null) {
			l += root.left.val;
		}
		return l + r;
	}

	private void travelsal(TreeNode root) {
		if (root == null) {
			return;
		}
		if (root.left != null && root.left.left == null && root.left.right == null) {
			sum += root.left.val;
		} else if (root.left != null) {
			travelsal(root.left);
		}
		if (root.right != null) {
			travelsal(root.right);
		}
	}

	public int sumOfLeftLeaves2(TreeNode root) {
		if (root == null) {
			return 0;
		}
		int l = sumOfLeftLeaves2(root.left);
		int r = sumOfLeftLeaves2(root.right);

		if (root.left != null && root.left.left == null && root.left.right == null) {
			l += root.left.val;
		}
		return l + r;
	}

}
